package org.xq.mq.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.Charset;

/**
 * 项目A调用项目B中的接口
 * 步骤1：在项目A中书写Http的工具类：
 * 步骤2：在application.yml中书写要调用的接口的目标路径（ip+端口+目录结构）
 * 步骤3：在controller层通过@Value注解引入配置文件中的接口路径，通过工具类调用接口
 * 步骤4：B项目中的controller：
 */
public class HttpUtil {
    public static String post(String url, String postBody) throws Exception {
//        System.out.println("========body: " + postBody);
//        System.out.println("========url: "+url);
        String result;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        // 设置超时时间
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(2000)
                // 报案接口超时时间建议设置为40秒
                .setSocketTimeout(40000).build();
        httpPost.setConfig(requestConfig);
        // 构造消息头，这个参数很重要，如果去掉，则返回的数据不能被json工具解析
        httpPost.setHeader("Accept", "application/json;charset=utf-8");
//        httpPost.setHeader("X-Api-Key", "ssuF8hqkxw13kBofcHiRGzMyQAXebLik");

        // 构建消息实体
        StringEntity entity = new StringEntity(postBody, Charset.forName("UTF-8"));
        entity.setContentEncoding("UTF-8");
        // 发送Json格式的数据请求
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        CloseableHttpResponse response = httpClient.execute(httpPost);

        result = EntityUtils.toString(response.getEntity(), "utf-8");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        jsonObject.put("statusLine",response.getStatusLine());

//        System.out.println("========result: " + result);
//        System.out.println("========statusLine: " + response.getStatusLine());
        response.close();
        httpClient.close();
        return jsonObject.toJSONString();
    }


    public static String get(String url) throws Exception {
//        System.out.println("========url: "+url);
        String result;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        // 设置超时时间
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(2000)
                // 报案接口超时时间建议设置为40秒
                .setSocketTimeout(40000).build();
        httpGet.setConfig(requestConfig);
        // 构造消息头，这个参数很重要，如果去掉，则返回的数据不能被json工具解析
        httpGet.setHeader("Accept", "application/json;charset=utf-8");
        httpGet.setHeader("X-Api-Key", "ssuF8hqkxw13kBofcHiRGzMyQAXebLik");

        CloseableHttpResponse response = httpClient.execute(httpGet);
        result = EntityUtils.toString(response.getEntity(), "utf-8");
//        System.out.println("========result: " + result);
//        System.out.println("========statusLine: " + response.getStatusLine());
        response.close();
        httpClient.close();
        return result;
    }
}
